<template>
	<div id='system' v-loading="loading">
		<div class="content-top">
			<span class="h1">业务管理</span>
			<span class="h2">商家需求</span>
		</div>
		<div class="top-btn clearfix">
			<div class="left-content">
				<span class="tip">商家需求</span>
				<el-radio-group v-model="rate" @change="change()">
					<el-radio :label="1">未审核</el-radio>
					<el-radio :label="2">已审核</el-radio>
				</el-radio-group>
				<span class="demonstration" style="margin-left: 15px; margin-right: 15px;">开始日期</span>
				<el-date-picker v-model="starTime" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd">
				</el-date-picker>
				<span class="demonstration" style="padding: 0 15px;">结束日期</span>
				<el-date-picker v-model="endTime" type="date" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" placeholder="选择日期">
				</el-date-picker>
			</div>
			<div class="right-search1">
				<el-input v-model="input" placeholder="输入关键词…"></el-input>
				<el-button type="primary" @click="look()" style="margin-left: 10px;">查看</el-button>
				<el-button type="primary" @click="excel()">导出excel</el-button>
			</div>
		</div>
		<el-table :data="tableData" style="width: 100%">
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column label="商家名称">
				<template slot-scope="scope">
					<span>{{ scope.row.ProviderInfo.PName }}</span>
				</template>
			</el-table-column>
			<el-table-column label="预充金额">
				<template slot-scope="scope">
					<span>{{ scope.row.ProviderInfo.PayMoney }}</span>
				</template>
			</el-table-column>
			<el-table-column label="负责人">
				<template slot-scope="scope">
					<span>{{ scope.row.ProviderInfo.ManagerMan}}</span>
				</template>
			</el-table-column>
			<el-table-column label="联系电话">
				<template slot-scope="scope">
					<span>{{ scope.row.ProviderInfo.Tel }}</span>
				</template>
			</el-table-column>
			<el-table-column prop="DemandDate" label="需求日期">
			</el-table-column>
			<el-table-column prop="DAmount" label="需求数量">
			</el-table-column>
			<el-table-column prop="StatusStr" label="状态">
			</el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope">
					<el-dropdown trigger="click">
						<span class="el-dropdown-link"><el-button type="primary">操作<i class="el-icon-arrow-down el-icon--right"></i></el-button></span>
						<el-dropdown-menu slot="dropdown" class="div">
							<el-dropdown-item>
								<el-button size="small" type="primary" @click="edits(scope.row.ID,scope.row.ProviderID,scope.row.DAmount)">修改</el-button>
							</el-dropdown-item>
							<el-dropdown-item>
								<el-button size="small" type="danger" @click="deleteRow(scope.row.ID)">删除</el-button>
							</el-dropdown-item>
							<el-dropdown-item>
								<el-button size="small" type="warning" @click="auditin(scope.row.ID)">拒绝</el-button>
							</el-dropdown-item>
							<el-dropdown-item>
								<el-button size="small" type="info" @click="auditin(scope.row.ID)">审核</el-button>
							</el-dropdown-item>
						</el-dropdown-menu>
					</el-dropdown>
				</template>
			</el-table-column>
		</el-table>
		<div class="pages">
			<el-pagination small :current-page="index" @current-change="handleCurrentChange" layout="prev, pager, next" :total="total" background prev-text='上一页' next-text='下一页'>
			</el-pagination>
		</div>
		<el-dialog title="修改需求" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
			<el-form ref="form" :model="form" label-width="100px">
				<el-form-item label="需求日期">
					<el-date-picker v-model="datatimes" disabled type="date" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" placeholder="选择日期">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="需求量">
					<el-input v-model="desc"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="quxia()">取 消</el-button>
				<el-button type="primary" @click="decline()">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import api from '../../../api/api'
	export default {
		name: 'provi',
		data() {
			return {
				datatimes: new Date(),
				form: {},
				desc: '',
				dialogVisible: false,
				rate: 1,
				index: 1,
				total: 1,
				loading: true,
				starTime: new Date(), //开始日期
				endTime: new Date(), //结束日期
				input: '', //搜索
				tableData: [], //商家列表
				id: "",
				pId: "",
			}
		},
		created() {
			this.getlistmneeds()
		},
		methods: {
			handleClose() {
				this.quxia()
			},
			quxia() {
				this.dialogVisible = false
			},
			//修改商家需求
			edits(id, pid, DAmount) {
				this.desc = DAmount
				this.dialogVisible = true
				this.id = id
				this.pId = pid
			},
			decline() {
				this.loading = true
				this.$axios.post(api.ledit, {
					"ID": this.id,
					"ProviderID": this.pId,
					"DemandDate": this.datatimes,
					"DAmount": this.desc
				}).then(res => {
					if(res.data.isOK) {
						this.dialogVisible = false
						this.getlistmneeds()
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/admin'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			excel() {
				this.loading = true
				this.$axios.post(api.getlistm, {
					"SDate": this.starTime,
					"EDate": this.endTime,
					"Status": this.rate,
					"pagePara": {
						"Para": this.input,
						"PageSize": 10,
						"PageIndex": this.index - 1,
						"OrderbyString": "PName",
						"ToExcel": true
					}
				}).then(res => {
					if(res.data.isOK) {
						if(res.data.data.ExcelResult && res.data.data.ExcelResult.isOK) {
							this.loading = false
							var urls = res.data.data.ExcelResult.data.filePath
							window.location.href = api.host + '/template/' + urls
						} else {
							this.loading = false
							this.$message.error(res.data.data.ExcelResult.msg);
						}
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/admin'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			//翻页
			handleCurrentChange(val) {
				this.index = val
				this.loading = true
				this.$axios.post(api.getlistm, {
					"SDate": this.starTime,
					"EDate": this.endTime,
					"Status": this.rate,
					"pagePara": {
						"Para": this.input,
						"PageSize": 10,
						"PageIndex": this.index - 1,
						"OrderbyString": "PName",
						"ToExcel": false
					}
				}).then(res => {
					if(res.data.isOK) {
						this.loading = false
						this.tableData = res.data.data.Data
						this.total = res.data.data.Items ? res.data.data.Items : 1
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/admin'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			handleEdit(index, row) {
				console.log(index, row);
			},
			handleDelete(index, row) {
				console.log(index, row);
			},
			//获取需求列表
			getlistmneeds() {
				this.loading = true
				this.$axios.post(api.getlistm, {
					"SDate": this.starTime,
					"EDate": this.endTime,
					"Status": this.rate,
					"pagePara": {
						"Para": this.input,
						"PageSize": 10,
						"PageIndex": this.index - 1,
						"OrderbyString": "PName",
						"ToExcel": false
					}
				}).then(res => {
					if(res.data.isOK) {
						this.loading = false
						this.tableData = res.data.data.Data
						this.total = res.data.data.Items ? res.data.data.Items : 1
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/admin'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			//审批
			auditin(id) {
				this.loading = true
				this.$axios.get(api.approve + '/' + id).then(res => {
					if(res.data.isOK) {
						this.loading = false
						this.getlistmneeds()
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/admin'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			//删除需求
			deleteRow(id) {
				this.loading = true
				this.$axios.get(api.del + '/' + id).then(res => {
					if(res.data.isOK) {
						this.loading = false
						this.getlistmneeds()
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/admin'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			//查看
			look() {
				this.getlistmneeds()
			},
			change() {
				this.loading = true
				this.index = 1
				this.$axios.post(api.getlistm, {
					"SDate": this.starTime,
					"EDate": this.endTime,
					"Status": this.rate,
					"pagePara": {
						"Para": "",
						"PageSize": 10,
						"PageIndex": this.index - 1,
						"OrderbyString": "PName",
						"ToExcel": false
					}
				}).then(res => {
					if(res.data.isOK) {
						this.loading = false
						this.tableData = res.data.data.Data
						this.total = res.data.data.Items ? res.data.data.Items : 1
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/admin'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			}
		}
	}
</script>

<style>

</style>